package com.reservation.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reservation.dto.ExhibitionCollectionDTO;
import com.reservation.entity.ArtCollection;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 藏品Mapper接口
 */
@Mapper
public interface CollectionMapper extends BaseMapper<ArtCollection> {
    // 使用 @Select 注解进行多表联查，并支持动态查询
    @Select("<script>" +
            "SELECT " +
            "e.id AS exhibition_id, e.name AS exhibition_name, " +
            "c.id , c.name , c.description , " +
            "c.image , c.era , c.type  " +
            "FROM exhibition e " +
            "JOIN exhibition_collection ec ON e.id = ec.exhibition_id " +
            "JOIN collection c ON ec.collection_id = c.id " +
            "WHERE c.deleted = 0 " +
            "<if test='exhibitionId != null'> AND e.id = #{exhibitionId} </if>" +
            "<if test='collectionName != null and collectionName != \"\"'> AND c.name LIKE CONCAT('%', #{collectionName}, '%') </if>" +
            "ORDER BY c.update_time DESC" +
            "</script>")
    IPage<ExhibitionCollectionDTO> getExhibitionWithCollections(
            Page<ExhibitionCollectionDTO> page,  // 分页参数
            @Param("exhibitionId") Long exhibitionId,
            @Param("collectionName") String collectionName
    );
}